package goldendawn.footballalarm.ui;

import java.util.ArrayList;

import godendawn.footballalarm.ui.R;
import godendawn.footballalarm.ui.R.id;
import goldendawn.core.DBManager;
import goldendawn.core.MapItem;
import goldendawn.footballalarm.adapter.GalleryAdapter;
import goldendawn.footballalarm.dao.TeamsHelper;
import goldendawn.footballalarm.entity.Teams;
import goldendawn.footballalarm.eume.Leagues;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

public class MainActivity extends Activity {
	private Gallery leagueGallery,teamsGallery;
	private GalleryAdapter leagueAdapter,teamAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_choose_favorite);
		initApp();
		initContrller();
		initData();
	}
	
	private void initContrller(){
		leagueGallery=(Gallery)findViewById(id.gallery_leagues);
		teamsGallery=(Gallery)findViewById(id.gallery_teams);
	}

	private void initData(){
		
		leagueAdapter=new GalleryAdapter(getApplicationContext(),getLeagues(), 1);
		leagueGallery.setAdapter(leagueAdapter);
		leagueGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				leagueAdapter.notifyDataSetChanged(arg2);
				String selectedId=(String)leagueAdapter.getItem(arg2);
				ArrayList<MapItem> lm= getTeams(Leagues.valueOf(Integer.valueOf(selectedId)));
				if(lm!=null){
					teamAdapter=new GalleryAdapter(getApplicationContext(), lm, 0);
					teamsGallery.setAdapter(teamAdapter);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		teamsGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				teamAdapter.notifyDataSetChanged(arg2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private ArrayList<MapItem> getLeagues(){
		TeamsHelper helper=new TeamsHelper();
		ArrayList<Leagues> ll= helper.getAllLeagues();
		ArrayList<MapItem> lm=new ArrayList<MapItem>();
		if(ll==null) return null;
		for (Leagues leagues : ll) {
			String imageN=null;
			switch (leagues) {
			case ENGLAND_PREMIERSHIP:
				imageN="premierleague.png";
				break;
			case ITALY_SERIA_A:
				imageN="serie_a.png";
				break;
			case ORHTERS:
				imageN="tim_cup.png";
				break;
			default:
				break;
			}
			lm.add(new MapItem(leagues.ordinal()+"", imageN));
		}
		return lm;
	}
	private ArrayList<MapItem> getTeams(Leagues league) {
		TeamsHelper helper=new TeamsHelper();
		ArrayList<Teams> lt=helper.getTeamsByLeague(league);
		if(lt==null) return null;
		ArrayList<MapItem> lm=new ArrayList<MapItem>();
		for (Teams teams : lt) {
			MapItem o=new MapItem(teams.getId()+"",teams.getImage_name());
			lm.add(o);
		}
		return lm;
	}
	private void initApp(){
		DBManager.createDB(getApplicationContext(), R.raw.fa);
	}
}
